/**
 * Charles 20161112
 */

$(function(){
	var urlStr=location.search;
	 var parObj = new Object();
    if (urlStr.indexOf("?") != -1) {
        urlPara = urlStr.substr(urlStr.indexOf("?") + 1).split("&");
    }
    for (var i = 0; i < urlPara.length; i++) {
        parObj[urlPara[i].split("=")[0].toString()] = urlPara[i].split("=")[1].toString();
    }
	var vmMain=new Vue({
		el: '#contentBody',
		data:{
			coursedetail:{},
			chapterlist:[],
			videopath:{},
			docList:[],
			conmentsList:[],
			Scores:0,
		},
		methods:{
			getDocList:function(){
				$.ajax({
			         type: "post",
			         url: "findDocResource",
			         dataType: "json",
			         data: { sectionId: parObj["sectionId"] },
			         success: function (result) {
			         	if(result)
			         	{
			         		for(var i=0;i<result.length;i++)
	         				{
			         			var fileStr=result[i].fileName.split('.');
			         			if(fileStr[fileStr.length-1].toLowerCase()=="doc"
			         				||fileStr[fileStr.length-1].toLowerCase()=="docx")
			         			{
			         				result[i].bgImg="kt.jpg";
			         				result[i].nameType="作业";
			         			}
			         			else if(fileStr[fileStr.length-1].toLowerCase()=="ppt"
			         				||fileStr[fileStr.length-1].toLowerCase()=="pptx")
			         			{
			         				result[i].bgImg="jy.jpg";
			         				result[i].nameType="讲义";
			         			}
			         			else{
			         				result[i].bgImg="undefined.png";
			         				result[i].nameType="作业";
			         			}
			         			result[i].filePath="\\atta\\"+result[i].filePath;
		         			}
			         		vmMain.docList=result;
			         	}
			         },
			         error: function () {
			             alert("data request errror!");
			             return;
			         }
			      });
			},
			getConmentsList:function(){
				$.ajax({
			         type: "post",
			         url: "findPinLunBySection",
			         dataType: "json",
			         data: { sectionId: parObj["sectionId"] },
			         success: function (result) {
			         	if(result)
			         	{
			         		vmMain.conmentsList=result;
			         	}
			         },
			         error: function () {
			             alert("data request errror!");
			             return;
			         }
			      });
			},
			downloadFile:function(filePath){
				if(!isLogin())
				{
					window.wxc.xcConfirm("请先登录再下载!", window.wxc.xcConfirm.typeEnum.info,{
						onOk:function(){
							
						}
					});
					return false;
				}
				else{
					try{
					   var items=filePath.split("\\");
					   var dname=items[items.length-1].split(".");
					   if(dname[dname.length-1]=="txt"){
						   var test=window.open(filePath,"","height=1px,width=1px,left=0,top=0,scrollbars=no,toolbars=no");//这个方法就直接把这个TXT以浏览器的方式打开了 
						   test.document.execCommand("SaveAs");
						   test.close();
					   }
					   else
					   {
						   var elemIF = document.createElement("iframe");   
				           elemIF.src = filePath;   
				           elemIF.style.display = "none";   
				           document.body.appendChild(elemIF);
					   }
					   insertLoadHistory(parObj["sectionId"],filePath);
					}
				   catch(e) 
				   { 
					   window.confirm(e.message);//window.confirm("下载完成."); 
				   } 
				}
			},
			setScore:function(val){
				vmMain.Scores=val;
				if(val==1){
					$("#score1").attr("class","stars2");
					$("#score2").attr("class","stars1");
					$("#score3").attr("class","stars1");
					$("#score4").attr("class","stars1");
					$("#score5").attr("class","stars1");
				}
				else if(val==2){
					$("#score1").attr("class","stars2");
					$("#score2").attr("class","stars2");
					$("#score3").attr("class","stars1");
					$("#score4").attr("class","stars1");
					$("#score5").attr("class","stars1");
				}
				else if(val==3){
					$("#score1").attr("class","stars2");
					$("#score2").attr("class","stars2");
					$("#score3").attr("class","stars2");
					$("#score4").attr("class","stars1");
					$("#score5").attr("class","stars1");
				}
				else if(val==4){
					$("#score1").attr("class","stars2");
					$("#score2").attr("class","stars2");
					$("#score3").attr("class","stars2");
					$("#score4").attr("class","stars2");
					$("#score5").attr("class","stars1");
				}
				else if(val==5){
					$("#score1").attr("class","stars2");
					$("#score2").attr("class","stars2");
					$("#score3").attr("class","stars2");
					$("#score4").attr("class","stars2");
					$("#score5").attr("class","stars2");
				}
			},
			sendConments:function(){
				var contents=$("#ccontent").val();
				if(contents==undefined||contents=="")
				{
					window.wxc.xcConfirm("评论内容不能为空", window.wxc.xcConfirm.typeEnum.info,{
						onOk:function(){
							$("#ccontent").focus();
						}
					});
				}
				else{
					$.ajax({
				        type: "post",
				        url: "saveStudyHistory",
				        dataType: "json",
				        async:false,
				        data: { sectionId: parObj["sectionId"],flag:"pinglun",content:contents,"xingxing":vmMain.Scores},
				        success: function (result) {
				        	$("#ccontent").val("");
				        	window.wxc.xcConfirm("评论成功", window.wxc.xcConfirm.typeEnum.success,{
				    			onOk:function(){
				    				$("#score1").attr("class","stars1");
									$("#score2").attr("class","stars1");
									$("#score3").attr("class","stars1");
									$("#score4").attr("class","stars1");
									$("#score5").attr("class","stars1");
				    				vmMain.getConmentsList();
				    			}
				    		});
				        },
				        error: function () {
				            return;
				        }
				     });
				}
			}
		},
	});
     var videoview=new Vue({
 		el:"#myvideo",
 		data:{videopath:{}},
 		template:'<video id="html5_api" controls="controls" autoplay="autoplay" preload="auto" style="margin-top:0;background:#000;"  width="100%" height="530px" :poster="videopath.imgPath"> <source :src="videopath.filePath" type="video/mp4"></video>',
 		methods:{
 			
 		}
 	});
    if(!isLogin())
	{
		window.wxc.xcConfirm("未登录或登录失效，请重新登录", window.wxc.xcConfirm.typeEnum.info,{
			onOk:function(){
				location.href="login";
			}
		});
		return false;
	}
    else{
    	$.ajax({
            type: "post",
            url: "findResource",
            async:false,
            dataType: "json",
            data: { sectionId: parObj["sectionId"] },
            success: function (result) {
            	if(result&&result.length>0&&result[0].filePath)
            	{
            		 result[0].filePath= "\\atta\\"+result[0].filePath;
            		 result[0].imgPath= "\\atta\\"+result[0].imgPath;
            		 result[0].sectionId=parObj["sectionId"];
            		 vmMain.videopath=result[0];
            		 videoview.videopath=result[0];
            	}
            },
            error: function () {
                alert("data request errror!");
                return;
            }
         });
        
   	$.ajax({
           type: "post",
           async:false,
           url: "findchaptersection",
           dataType: "json",
           data: { id: parObj["id"] },
           success: function (result) {
           	if(result&&result.length>0)
       		{
           		vmMain.coursedetail=result[0];
       		    if(result.length>1)
   		    	{
   		    	    for(var i=1;i<result.length;i++)
   	    	    	{
   		    	    	if(result[i].chapter.length>0)
   	    	    		{
   		    	    		vmMain.chapterlist.push(result[i]);
   	    	    		}
   		    	    	    
   	    	    	}
   		    	}
       		}
           },
           error: function () {
               alert("data request errror!");
               return;
           }
       });
    } 
});

var checkplay=function(id)
{
	if(!isLogin())
	{
		window.wxc.xcConfirm("未登录或登录失效，请重新登录!", window.wxc.xcConfirm.typeEnum.info,{
			onOk:function(){
				location.href="login";
			}
		});
		return false;
	}
	var urlStr=location.search;
	var parObj = new Object();
    if (urlStr.indexOf("?") != -1) {
        urlPara = urlStr.substr(urlStr.indexOf("?") + 1).split("&");
    }
    for (var i = 0; i < urlPara.length; i++) {
        parObj[urlPara[i].split("=")[0].toString()] = urlPara[i].split("=")[1].toString();
    }
	$.ajax({
        type: "post",
        url: "findResource",
        async: false,
        dataType: "json",
        data: { sectionId: id },
        success: function (result) {
        	if(result&&result.length>0&&result[0].filePath)
        	{
        		if(result[0].filePath)
        		{
        			location.href = 'onlinecourse?id='+parObj["id"]+'&sectionId='+id;
        		}
        		else
        			window.wxc.xcConfirm("该章节暂无视频资源!", window.wxc.xcConfirm.typeEnum.info,{
        				onOk:function(){
        					
        				}
        			});

        	}
        	else{
        		window.wxc.xcConfirm("该章节暂无视频资源!", window.wxc.xcConfirm.typeEnum.info,{
    				onOk:function(){
    					
    				}
    			});
        	}
            
        },
        error: function () {
        	window.wxc.xcConfirm("data request error!", window.wxc.xcConfirm.typeEnum.info,{
				onOk:function(){
					return false;
				}
			});
            return;
        }
     });
};
var insertLoadHistory=function(id,filePath){
	$.ajax({
		type: "post",
	    url: "saveMyDownLoadHistory",
	    async: false,
	    dataType: "json",
	    data: { sectionId: id ,filePath:filePath},
	    success: function (result) {
	    	
	    },
	    error:function(result){
	    	
	    }
	});
};
var getMesg=function()
	{
	window.wxc.xcConfirm("资源无效!", window.wxc.xcConfirm.typeEnum.info,{
		onOk:function(){
			
		}
	});
	};